package edu.gmu.client;

import edu.gmu.messages.EchoRequest;
import edu.gmu.middleware.ClientDispatcher;
import edu.gmu.middleware.MessageEvent;
import edu.gmu.middleware.MessageListener;
import edu.gmu.middleware.Sink;
import edu.gmu.middleware.SinkFactory;
import java.io.IOException;

public class SubscribeClient
{
 
    public static void main( String[] args ) throws IOException, InterruptedException
    {
        String remoteAddress = "localhost";
        int remotePort       = Integer.parseInt(args[0]);
        
        Sink sink = SinkFactory.makeTCPSink( remoteAddress, remotePort );
        final ClientDispatcher dispatcher = new ClientDispatcher(sink, 9, "SubscribeClient");
        
        
        
        dispatcher.addMessageListener( new MessageListener()
        {
            public void messageReceived( MessageEvent event )
            {
                EchoRequest m = (EchoRequest)event.getMessage();
                int[] payload = m.payload;
                System.out.println( "count="+m.count+" missive="+m.missive +
                        " payload[size]="+payload[payload.length-1] );
//                try
//                {
//
//                    dispatcher.unsubscribe(100);
//                }
//                catch (IOException ex)
//                {
//                    ex.printStackTrace();
//                }
            }
        });
        dispatcher.start();
        sink.connect();
        
        dispatcher.subscribe(100, EchoRequest.class);
    }
}